package com.lin.blog.controller;


import com.lin.blog.entity.Tags;
import com.lin.blog.service.ITagsService;
import com.lin.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Api(tags = "Tags接口")
@RestController
@RequestMapping("/tags")
public class TagsController {

    @Autowired
    private ITagsService tagsService;


    /**
     * 通过主键ID查询Tags
     * @param id
     * @return
     */
    @ApiOperation("通过主键ID查询用Tags")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @GetMapping("/{id}")
    public Result getTagsById(@PathVariable(value = "id",required = true) Long id){
        return tagsService.getTags(id);
    }

    /**
     * 插入一条Tags记录
     * @param tags
     * @return
     */
    @RequiresPermissions("tags:add")
    @ApiOperation("插入一条Tags记录")
    @PostMapping
    public Result insertTags(@RequestBody @Valid Tags tags){
        return tagsService.saveTags(tags);
    }


    /**
     * 通过主键ID删除Tags
     * @param id
     * @return
     */
    @RequiresPermissions("tags:delete")
    @ApiOperation("通过主键ID删除Tags")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @DeleteMapping("/{id}")
    public Result deleteTagsById(@PathVariable(value = "id",required = true) Long id){
        return tagsService.removeTags(id);
    }

    /**
     * 通过主键ID更新Tags
     * @param id
     * @param tags
     * @return
     */
    @RequiresPermissions("tags:update")
    @ApiOperation("通过主键ID更新Tags")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @PutMapping("/{id}")
    public Result updateTags(@PathVariable(value = "id",required = true) Long id,@RequestBody Tags tags){
        return tagsService.updateTags(id,tags);
    }



    /**
     * 获取TagsList
     * @return
     */
    @ApiOperation("获取TagsList")
    @GetMapping("/list")
    public Result  getTagsList(){
        return tagsService.listTags();
    }


    /**
     * 分页获取TagsList
     * @return
     */
    @ApiOperation("分页获取TagsList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条", defaultValue = "20")})
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result  getTagsList(@PathVariable("pageNum")  Long pageNum,
                                   @PathVariable("pageSize") Long pageSize){
        return tagsService.listTags(pageNum, pageSize);

    }
}
